<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    //分类添加
    public function getAdd()
    {
    	//查询分类
    	//$cates = DB::table('cate')->get();

        $cates = self::CateList(); 

    	return view('admin.cate.add',['cate'=>$cates]);
    } 

    //执行添加
    public function postInsert(Request $request)
    {

    	//dd($request->all());
    	$data = $request->only(['name','pid']);
        //判断pid是否为O
        if($data['pid'] == 0){
            $data['path'] = '0'; 
        }else{
            $res = DB::table('cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }

        $data['status'] = '1';
        //dd($data);
    	//拼接数据
    	//执行插入
    	$res = DB::table('cate')->insert($data);
        //dd($res);
    	if($res){
    		return redirect('/admin/cate/index')->with('success','分类添加成功');
    	}else{
    		return back()->with('error','分类添加失败');
    	}
    }

    //分类列表
    public function getIndex()
    {   
        $cates = self::CateList(); 
        //$cates = DB::table('cate')->get();
         foreach ($cates as $k => $v) {
            if($v->pid !=0){
              $res = DB::table('cate')->where('id',$v->pid)->first();
              $cates[$k]->path = $res->name;
           }else{
                $cates[$k]->path = '顶级';
           }
     }
    	return view('admin.cate.index',['cates'=>$cates]);
    }
    static public function CateList()
    {
       $cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
        //dd($cates);
        foreach ($cates as $k => $v) {
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|--',$len).$v->name;
        }
           return $cates;
    }

    //删除
    public function postDelete(Request $request)
    {
        $id = $request->input('id');
        $res = DB::table('cate')->where('pid',$id)->count();
        if($res > 0 ){
            //不能删除
            return 1;
        }
        //执行删除
        $res = DB::table('cate')->where('id',$id)->delete();
        if($res){
            //成功
            echo 2;
        }else{
            //失败
            echo 0;
        }
    }

        //分类修改
        public function getEdit($id)
        {
            $cates = self::CateList();
            $cate = DB::table('cate')->where('id',$id)->first();
            return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates]);
        }

        //分类数据修改
        public function postUpdate(Request $request)
        {
            //查看是否有子类
            $id = $request->input('id');

            //判断
            if($request->input('pid') != $request->input('xpid')){
                $res = DB::table('cate')->where('pid',$id)->count();
                if($res > 0){
                    return back()->with('error','有子分类,不能修改当前分类');
                }
            }

            $data = $request->only(['name','pid']);

            //判断pid
            if($data['pid'] == 0){
                $data['path'] = '0'; 
            }else{
                $res = DB::table('cate')->where('id',$data['pid'])->first();
                $data['path'] = $res->path.','.$data['pid'];
            }

            $data['status'] = '1';

            $res = DB::table('cate')->where('id',$id)->update($data);
        //dd($res);
            if($res){
                return redirect('/damin/cate/index')->with('success','分类修改成功');
            }else{
                return back()->with('error','分类修改失败');
            }



        }
}
